package de.LooLo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @author Kevin Seppelt
 * Detail Window for one loo
 *
 */
public class DetailActivity extends Activity {

	private int looID;
	private ImageView typeImg;
	
	/**
	 * Toilet for which the detail view is created
	 */
	private Toilet loo;
	
	/**
	 * Toilet type
	 */
	private TextView type;
	
	/**
	 * ListView for additional information
	 */
	private ListView liste;
	
	/**
	 * List of additional information
	 */
	private List<CvDetailElement> cvDetailList = new ArrayList<CvDetailElement>();
	
	/**
	 * Additional information as serialized tuples (description,value)
	 */
	private ArrayList<String> items = new ArrayList<String>();
	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail_view);
		
		looID = getIntent().getExtras().getInt("loo_id",0);
		loo = null;
		
		ArrayList<Toilet> loos = DatabaseManagement.getInstance().selectAllLoos();
		for (int i = 0; i < loos.size(); i++){
			if (loos.get(i).getId() == looID)
				loo = loos.get(i);
		}
		if (loo == null){
			finish();
		}
		
		type = (TextView) this.findViewById(R.id.detail_type);
		typeImg = (ImageView) this.findViewById(R.id.type_icon);
		
		if(loo.insideBuilding){		
			type.setText(R.string.building_toilet_string);
			typeImg.setImageResource(R.drawable.loobuilding_small_icon);
		}
		
		
		liste = (ListView) findViewById(R.id.listView);
		
		//read additional information
			if(!(loo.getName().equals("NaN"))){
				items.add("Name:");
				items.add(loo.getName());
			}
			if(!(loo.getFee().equals("NaN"))){
				items.add("Fee:");
				items.add(loo.getFee());
			}
			if(!(loo.getWheelchair().equals("NaN"))){
				items.add("Wheelchair:");
				items.add(loo.getWheelchair());
			}
			if(!(loo.getLayer().equals("NaN"))){
				items.add("Layer:");
				items.add(loo.getLayer());
			}
			if(!(loo.getSupervised().equals("NaN"))){
				items.add("Supervised:");
				items.add(loo.getSupervised());
			}
			if(!(loo.getOpening_hours().equals("NaN"))){
				items.add("Opening Hours:");
				items.add(loo.getOpening_hours());
			}
		
		
		if(items.size()!=0) initListView();
		
	}
	
	/**
	 * inits ListView with additional information (if available)
	 */
	public void initListView(){
			
			cvDetailList.clear();
			
			for (int i = 0; i < items.size(); i = i+2){
				cvDetailList.add(new CvDetailElement(this));
			}
			
			ListAdapter listeAdapter = new CvDetailElementAdapter(this, cvDetailList, loo, items);
			liste.setAdapter(listeAdapter);
			
			liste.setItemsCanFocus(false);
		}

}
